<template>
  <div>
    <a-form-model-item label="用户"
                       prop="">
      <z-select-user @change="userChange"
                     v-model="newValue.userList" />
    </a-form-model-item>
    <a-form-model-item label="角色"
                       prop="">
      <z-select-role @change="roleChange"
                     v-model="newValue.roleList" />
    </a-form-model-item>
    <a-form-model-item label="单位"
                       prop="">
      <z-select-unit @change="unitChange"
                     placeholder="请选择单位"
                     v-model="newValue.unitList" />
    </a-form-model-item>
  </div>
</template>

<script>
import PropTypes from '@zh/common-utils/prop-types'
import _isEqual from 'lodash/isEqual'

export default {
  name: 'ScheduleUnitUserRole',
  props: {
    value: {
      type: Object,
      default: () => {
        return {
          newValue: {
            unitList: [],
            userList: [],
            roleList: [],
            memberUsers: [],
            memberUnits: [],
            memberRoles: []
          }
        }
      }
    },
    form: {
      type: Object,
      default: () => ({})
    }
    // propsUnitChange: PropTypes.func.def(undefined),
    // propsRoleChange: PropTypes.func.def(undefined),
    // propsUserChange: PropTypes.func.def(undefined)
  },
  // computed: {
  //   taskRecipientValue() {
  //     return {
  //       unitList: this.unitList,
  //       userList: this.userList,
  //       roleList: this.roleList,
  //       memberUsers: this.memberUsers,
  //       memberUnits: this.memberUnits,
  //       memberRoles: this.memberRoles
  //     }
  //   }
  // },
  data () {
    return {
      forms: {},
      newValue: {
        unitList: [],
        userList: [],
        roleList: [],
        memberUsers: [],
        memberUnits: [],
        memberRoles: []
      }
    }
  },
  watch: {
    value: {
      handler (value, oldValue) {
        if (!_isEqual(value, oldValue)) {
          this.newValue = value
        }
      },
      immediate: true,
      deep: true
    },
    newValue: {
      handler (value, oldValue) {
        this.$emit('input', value)
      },
      immediate: true,
      deep: true
    }

  },
  methods: {
    unitChange (all, unit) {
      let memberUnits = []
      for (let j = 0; j < unit.length; j++) {
        const ut = unit[j]
        memberUnits.push({
          memberId: ut.id,
          memberName: ut.name,
          type: 2
        })
      }
      this.newValue = {
        ...this.newValue,
        memberUnits: memberUnits
      }
    },
    userChange (all, user) {
      let memberUsers = []
      for (let j = 0; j < user.length; j++) {
        const us = user[j]
        memberUsers.push({
          memberId: us.id,
          memberName: us.name,
          type: 1
        })
      }
      this.newValue = {
        ...this.newValue,
        memberUsers: memberUsers
      }
    },
    roleChange (all, role) {
      let memberRoles = []
      for (let j = 0; j < role.length; j++) {
        const ul = role[j]
        memberRoles.push({
          memberId: ul.id,
          memberName: ul.name,
          type: 3
        })
      }
      this.newValue = {
        ...this.newValue,
        memberRoles: memberRoles
      }
    }
  }
}
</script>

<style scoped>
</style>
